//
//  DDBossPublishedJobViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/21.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossPublishedJobViewController.h"
#import "DDBossPublishJobViewController.h"
#import "DDBossWorkDetailViewController.h"
#import "DDPublishedJobCell.h"
#import "DDBossJobService.h"
#import "DDBossPublishedJobModel.h"

@interface DDBossPublishedJobViewController ()

@property (strong, nonatomic) UIButton *onComputerEditButton;

@property (strong, nonatomic) UIButton *publishJobButton;

@property (strong, nonatomic) DDBossJobService *jobService;

@end

@implementation DDBossPublishedJobViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"我发布过的职位";
    
    self.jobService = [[DDBossJobService alloc] init];
    
    [self setupView];

    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
    [self requestMyPublishedJobsInfo];
}


#pragma mark
#pragma mark - Private Methods

- (void)setupView
{
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.rowHeight = 55;
    
    [self.view addSubview:self.publishJobButton];
    
    WEAKSELF
    [self.publishJobButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view);
        make.height.equalTo(@50);
    }];

    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view);
        make.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view);
        make.bottom.mas_equalTo(weakSelf.publishJobButton.mas_top);
    }];
}

#pragma mark
#pragma mark - Event Response

- (void)onPublishButtonClick:(UIButton *)button
{
    DDBossPublishJobViewController *pulishJobVC = [[DDBossPublishJobViewController alloc] init];
    
    [self.navigationController pushViewController:pulishJobVC animated:YES];
}

- (void)onComputerEditButtonClick:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:STR_MANAGER_ON_COMPUTER delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alert show];
}

- (void)requestMyPublishedJobsInfo
{
    [self.dataSource removeAllObjects];
    WEAKSELF
    [self.jobService queryMyselfAllJobInfoSuccessBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            
            DDBossPublishedJobModel *model = [DDBossPublishedJobModel yy_modelWithJSON:responseObject];
            [weakSelf.dataSource addObjectsFromArray:model.data];
            [weakSelf.tableView reloadData];
            
        } else {
//            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

#pragma mark
#pragma mark - CustomDelegate

#pragma mark -
#pragma mark  UITableViewDataSource

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 40;
//}
//
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
//    headerView.userInteractionEnabled = YES;
//    if (_onComputerEditButton == nil) {
//        _onComputerEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        _onComputerEditButton.frame = headerView.bounds;
//        [_onComputerEditButton setBackgroundColor:[UIColor colorWithHexString:@"fffb93"]];
//        
//        [_onComputerEditButton setTitle:@"在电脑上管理职位 >" forState:UIControlStateNormal];
//        [_onComputerEditButton setTitleColor:[UIColor colorWithHexString:@"daa34e"] forState:UIControlStateNormal];
//        [_onComputerEditButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
//        
//        [_onComputerEditButton addTarget:self action:@selector(onComputerEditButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    }
//    [headerView addSubview:_onComputerEditButton];
//    return headerView;
//}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.title = [NSString stringWithFormat:@"已发布了%ld个职位",self.dataSource.count];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"titles" object:self.title];
    return [self.dataSource count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DDPublishedJobCell";
    
    DDPublishedJobCell *cell = (DDPublishedJobCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDPublishedJobCell" owner:self options:nil] lastObject];
    }
    
    if (self.dataSource.count > indexPath.row) {
        DDBossPublishedJobInfo *jobInfo = (DDBossPublishedJobInfo *)self.dataSource[indexPath.row];
        
        [cell configureJobCellWithInfo:jobInfo];
    }
    
    return cell;
}


#pragma mark -
#pragma mark  UITableViewDelegate   

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DDBossPublishedJobInfo *jobInfo = (DDBossPublishedJobInfo *)self.dataSource[indexPath.row];
    
    DDBossWorkDetailViewController *detailVC = [[DDBossWorkDetailViewController alloc] init];
    detailVC.jobId = jobInfo.jobid;
    detailVC.title = jobInfo.jobtitle;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark
#pragma mark - Getters and Setters

- (UIButton *)publishJobButton
{
    if (_publishJobButton == nil) {
        _publishJobButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _publishJobButton.layer.borderColor = COLOR_BORDER_GRAY.CGColor;
        _publishJobButton.layer.borderWidth = 1;
        [_publishJobButton setImage:[UIImage imageNamed:@"boss_icon_position"] forState:UIControlStateNormal];
        [_publishJobButton setBackgroundColor:[UIColor whiteColor]];
        [_publishJobButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_publishJobButton setTitle:@"发布职位" forState:UIControlStateNormal];
        [_publishJobButton setTitleColor:COLOR_NAVIGATIONBAR forState:UIControlStateNormal];
        [_publishJobButton.titleLabel setFont:[UIFont systemFontOfSize:15]];

        [_publishJobButton addTarget:self action:@selector(onPublishButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _publishJobButton;
}

@end
